﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Util;
using System.Web.Mail;
using CNVP.Framework;
using CNVP.Config;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Member
{
    public partial class RetrievePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region js验证
                txtUserNameRetrievePwd.Attributes.Add("onFocus", "RegTipInfo('lbUserName','请输入会员账户．')");
                txtUserNameRetrievePwd.Attributes.Add("onBlur", "return RegCheck('txtUserNameRetrievePwd','lbUserName')");
                txtEmail.Attributes.Add("onFocus", "RegTipInfo('lbEmail','请输入Email．')");
                txtEmail.Attributes.Add("onBlur", "return RegCheck('txtEmail','lbEmail')");
                btUp.Attributes.Add("onClick", "return RetrievePassword()");
                #endregion
            }
        }
        protected void btUp_Click(object sender, EventArgs e)
        {
            string _UserName = Request.Form["txtUserNameRetrievePwd"];
            string _Email= Request.Form["txtEmail"];
            if (string.IsNullOrEmpty(_UserName))
            {
                BasePage.MessageBoxGoHistory("请输入用户名！");
            }
            else if (!Public.IsEmail(_Email) || string.IsNullOrEmpty(_Email))
            {
                BasePage.MessageBoxGoHistory("请输入邮箱！");
            }
            else if (string.IsNullOrEmpty(Request.Form["txtVerifyCode"]))
            {
                BasePage.MessageBoxGoHistory("请输入验证码！");
            }
            else
            {
                if (Session["CheckCode"] != null)
                {
                    if (Session["CheckCode"].ToString() == Request.Form["txtVerifyCode"].Trim().ToLower())
                    {
                        Model.CNVP_Users model = new BLL.CNVP_Users().GetSingle_CNVP_UsersByUserName(_UserName, _Email);
                        if (model != null)
                        {
                            //添加密码信息
                            Guid RetrievePwdInfo_ID = Guid.NewGuid();
                            new BLL.CNVP_RetrievePwdInfo().Add_CNVP_RetrievePwdInfo(new Model.CNVP_RetrievePwdInfo()
                            {
                                ID = RetrievePwdInfo_ID,
                                PostTime = DateTime.Now,
                                UserName = _UserName
                            });

                            MailUtils _MailUtils = new MailUtils();
                            _MailUtils.SmtpServer = UIConfig.MailServer;
                            _MailUtils.UserName = UIConfig.MailUserName;
                            _MailUtils.UserPass = UIConfig.MailUserPass;
                            string toSubject = UIConfig.SiteName + "_" + _UserName + "_忘记密码";
                            string toBody = UIConfig.SiteDomain + "/Member/ReSetPassword.aspx?ID=" + RetrievePwdInfo_ID;
                            bool flag = _MailUtils.Send(_Email, UIConfig.MailInfo, toSubject, toBody, false);

                            if (flag == true)
                            {
                                BasePage.MessageBox("发送成功，查看您的邮箱！", "../index.aspx");
                            }
                            else
                            {
                                BasePage.MessageBox("发送失败！", "RetrievePassword.aspx");
                            }
                        }
                        else
                        {
                            BasePage.MessageBoxGoHistory("您没有这个用户名或邮箱！");
                        }
                    }
                    else
                    {
                        BasePage.MessageBoxGoHistory("输入验证码有误！");
                    }
                }
            }
        }
    }
}